package cn.net.dpi.util;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Copyright(c) 2021 Dayou Positioning Intelligence Co.,Ltd
 * All rights reserved.
 *
 * @Date 2021/9/16
 * @Author caoxiangshang(caoxiangshang @ dpi.net.cn)
 * @Version v0.1
 * @Reviewer
 */
@Service
public class RedisUtils {
    @Resource(name = "redisObjectTemplate")
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisTemplate<String, Object> redisHashTemplate;

    @Autowired
    private RedisTemplate redisTransactionTemplate;

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */
    public boolean setTransaction(final String key, Object value) {
        boolean result = false;
        try {
            redisTransactionTemplate.opsForValue().set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 写入缓存设置时效时间
     *
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime, TimeUnit timeUnit) {
        boolean result = false;
        try {
            redisTemplate.opsForValue().set(key, value);
            redisTemplate.expire(key, expireTime, timeUnit);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern
     */
    public void removePattern(final String pattern) {
        redisTemplate.delete(redisTemplate.keys(pattern));
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    public void remove(final String key) {
        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */
    public boolean exists(String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 哈希 添加
     *
     * @param key key值
     * @param hashKey 阈值
     * @param value value值
     */
    public void hmSet(String key, Object hashKey, Object value) {
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        hash.put(key, hashKey, value);
    }

    /**
     * 哈希获取数据
     *
     * @param key key值
     * @param hashKey 阈值
     * @return 获取的hash数据
     */
    public Object hmGet(String key, Object hashKey) {
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        return hash.delete(key,hashKey);
    }

    /**
     * 哈希获取数据
     *
     * @param key key值
     * @param hashKeys 个数可变阈值
     * @return 返回删除的数
     */
    public Object hDel(String key, Object...  hashKeys) {
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        return hash.delete(key, hashKeys);
    }


    /**
     * 列表添加
     *
     * @param k
     * @param v
     */
    public void lPush(String k, Object v) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        list.rightPush(k, v);
    }

    /**
     * 列表获取
     *
     * @param k
     * @param l
     * @param l1
     * @return
     */
    public List<Object> lRange(String k, long l, long l1) {
        ListOperations<String, Object> list = redisTemplate.opsForList();
        return list.range(k, l, l1);
    }

    /**
     * 集合添加
     *
     * @param key
     * @param value
     */
    public void add(String key, Object value) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        set.add(key, value);
    }

    /**
     * 集合获取
     *
     * @param key
     * @return
     */
    public Set<Object> setMembers(String key) {
        SetOperations<String, Object> set = redisTemplate.opsForSet();
        return set.members(key);
    }

    /**
     * 有序集合添加
     *
     * @param key
     * @param value
     * @param scoure
     */
    public void zAdd(String key, Object value, double scoure) {
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        zset.add(key, value, scoure);
    }

    /**
     * 有序集合获取
     *
     * @param key
     * @param scoure
     * @param scoure1
     * @return
     */
    public Set<Object> rangeByScore(String key, double scoure, double scoure1) {
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        return zset.rangeByScore(key, scoure, scoure1);
    }

    /**
     * hash类型参数存储
     *
     * @param key   哈希表 key
     * @param field 域 field
     * @param value 域 field 的值
     */
    public void hset(String key, Object field, Object value) {
        HashOperations<String, Object, Object> opsForHash = redisHashTemplate.opsForHash();
        opsForHash.put(key, field, value);
    }

    /**
     * 查看哈希表 key 中，给定域 field 是否存在
     *
     * @param key   哈希表 key
     * @param field 域 field
     * @return 返回值：如果 field 存在，返回 1， 其他返回 0
     */
    public Boolean hexists(String key, Object field) {
        HashOperations<String, Object, Object> opsForHash = redisHashTemplate.opsForHash();
        return opsForHash.hasKey(key, field);
    }

    /**
     * 获取哈希表 key 中给定域 field 的值
     *
     * @param key   哈希表 key
     * @param field 域 field
     * @return field 域的值，如果 key 不存在或者 field 不存在返回 nil
     */
    public Object hget(String key, Object field) {
        HashOperations<String, Object, Object> opsForHash = redisHashTemplate.opsForHash();
        return opsForHash.get(key, field);
    }

    /**
     * 获取哈希表key中所有的域和值
     *
     * @param key key
     * @return 以列表形式返回hash中域和域的值 ，key不存在，返回空hash
     */
    public Object hgetall(String key) {
        HashOperations<String, Object, Object> opsForHash = redisHashTemplate.opsForHash();
        return opsForHash.entries(key);
    }

    public Long zrem(String key, Object field) {
        ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
        return zset.remove(key,field);
    }
}